package com.kmvc.message.activity;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.kmvc.message.R;
import com.kmvc.ui.IphoneTitleBarActivity;

public class AutoClearActivity extends IphoneTitleBarActivity implements
		OnCheckedChangeListener {
	private RadioGroup radioGroup;
	private TextView isClearInfo;
	private CheckBox isClear;
	private SharedPreferences sp;
	private Editor editor;
	private String checkedString;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.auto_clear);
		setTitle("信息自动清理");
		setLeftButton(R.string.goback, null);

		sp = getSharedPreferences("com.kmvc.message_preferences", 0);
		editor = sp.edit();

		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		isClearInfo = (TextView) findViewById(R.id.isClearInfo);
		isClear = (CheckBox) findViewById(R.id.isClear);
		
		if (sp.getBoolean("isclear", false) == true) {
			isClear.setChecked(true);
			radioGroup.setEnabled(true);
		} else {
			isClear.setChecked(false);
			radioGroup.setEnabled(false);
		}

		CheckIsClear();

		isClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				CheckIsClear();
			}

		});
		radioGroup.setOnCheckedChangeListener(this);
	}

	private void CheckIsClear() {
		if (isClear.isChecked()) {
			radioGroup.setVisibility(View.VISIBLE);
			editor.putBoolean("isclear",true);
			editor.commit();
			isClearInfo.setText("是否自动清理(已启用)");
			checkedString = sp.getString("timelist", "");
			for (int i = 0; i < 6; i++) {
				if (checkedString.equals(i+"")) {
					radioGroup.check(i+R.id.radio0);
				}
			}
		} else {
			radioGroup.setVisibility(View.GONE);
			isClearInfo.setText("是否自动清理(未启用)");
			editor.putBoolean("isclear", false);
			editor.commit();
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup arg0, int i) {
		editor.putString("timelist", ""+(i - R.id.radio0));
		editor.commit();
	}
}
